import { smartTranslate as $t, transPlaceholder as $p } from '@/lang';
export function useTools(ToolAPI) {
  // 清理系统软删除的数据
  const clearSysDeletedData = async () => {
    try {
      ElMessageBox.confirm(
        $t('menu.admin.tool.desc.clear_sys_deleted_data') +
          $t('common.message.confirm.general', {
            action: $t('common.action.delete'),
          }),
        $t('common.title.warning'),
        {
          confirmButtonText: $t('common.action.confirm'),
          cancelButtonText: $t('common.action.cancel'),
          type: 'warning',
        },
      ).then(async () => {
        const { data } = await ToolAPI.clearSysDeletedData();
        const counters = Object.values(data).reduce(
          (acc, { status }) => {
            if (status === 'skipped') acc.skipped++;
            if (status === 'success') acc.success++;
            return acc;
          },
          { skipped: 0, success: 0 },
        );
        let message = $t('menu.admin.tool.result', {
          skipped: counters.skipped,
          success: counters.success,
        });
        ElMessageBox.alert(
          message,
          $t('commom.message.success.general', {
            action: $t('menu.admin.tool.action.clear_sys_deleted_data'),
          }),
          { type: 'success' },
        );
      });
    } catch (error) {
      ElMessageBox.alert(
        error,
        $t('commom.message.failed.general', {
          action: $t('menu.admin.tool.action.clear_sys_deleted_data'),
        }),
        { type: 'error' },
      );
    }
  };

  // 删除thinkphp runtime 目录中的日志
  const clearRuntimeLogs = async () => {
    try {
      ElMessageBox.confirm(
        $t('menu.admin.tool.desc.clear_runtime_logs') +
          $t('common.message.confirm.general', {
            action: $t('common.action.delete'),
          }),
        $t('common.title.warning'),
        {
          confirmButtonText: $t('common.action.confirm'),
          cancelButtonText: $t('common.action.cancel'),
          type: 'warning',
        },
      ).then(async () => {
        const { data } = await ToolAPI.clearRuntimeLogs();
        let message = $t('menu.admin.tool.result', {
          success: data.success,
          fail: data.fail,
        });
        ElMessageBox.alert(
          message,
          $t('commom.message.success.general', {
            action: $t('menu.admin.tool.action.clear_runtime_logs'),
          }),
          { type: 'success' },
        );
      });
    } catch (error) {
      ElMessageBox.alert(
        error,
        $t('commom.message.failed.general', {
          action: $t('menu.admin.tool.action.clear_runtime_logs'),
        }),
        { type: 'error' },
      );
    }
  };

  return {
    clearSysDeletedData,
    clearRuntimeLogs,
  };
}
